package com.hm.system.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.common.common.DateUtil;
import com.hm.system.util.SystemConfigDef.Api.Rest;
import com.hm.system.web.su.BaseController;

import io.swagger.annotations.ApiOperation;

/**
 * @author shishun.wang
 * @date 下午4:25:34 2016年8月6日
 * @version 1.0
 * @describe
 */
@ResponseBody
@RestController
@RequestMapping(value = Rest.Sys.REST_SYS_REAL_TIME_PUSH)
public class RealTimePushController extends BaseController {

	public static final String swaggerTag = "消息推送";

	@RequestMapping(value = "/server/time", method = RequestMethod.GET)
	@ApiOperation(value = "实时推送数据", httpMethod = "GET", notes = "服务器实时推送系统当前时间到客户端展示", tags = swaggerTag)
	public void currentTime() {
		try {
			response.setContentType("text/event-stream");
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			
			PrintWriter writer = response.getWriter();
			{
				writer.write("data:" + DateUtil.yyyyMMddhhmm(new Date())+"\n\r");
				writer.flush();
				writer.close();
			}
			Thread.sleep(100);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
